package v1

import (
	"net/http"
	"toy-car/models"
	"toy-car/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

// @Tags SysDictionaryDetail
// @Summary 创建SysDictionaryDetail
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionaryDetail true "SysDictionaryDetail模型"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /sysDictionaryDetail/createSysDictionaryDetail [post]
func DictionaryDetailAdd(c *gin.Context) {
	var detail models.SysDictionaryDetail
	_ = c.ShouldBindJSON(&detail)

	code := e.SUCCESS
	msg := e.GetMSG(code)

	err := models.CreateSysDictionaryDetail(detail)
	if err != nil {
		code = e.ERROR
		msg = err.Error()
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  msg,
		"data": true,
	})
}

// @Tags SysDictionaryDetail
// @Summary 删除SysDictionaryDetail
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionaryDetail true "SysDictionaryDetail模型"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sysDictionaryDetail/deleteSysDictionaryDetail [delete]
func DictionaryDetailDelete(c *gin.Context) {
	var detail models.SysDictionaryDetail
	_ = c.ShouldBindJSON(&detail)

	id := com.StrTo(c.Param("id")).MustInt()
	detail.ID = id
	code := e.SUCCESS
	err := models.DeleteSysDictionaryDetail(detail)
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": true,
	})
}

// @Tags SysDictionaryDetail
// @Summary 更新SysDictionaryDetail
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionaryDetail true "更新SysDictionaryDetail"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sysDictionaryDetail/updateSysDictionaryDetail [put]
func DictionaryDetailUpdate(c *gin.Context) {
	var detail models.SysDictionaryDetail
	_ = c.ShouldBindJSON(&detail)

	code := e.SUCCESS
	err := models.UpdateSysDictionaryDetail(&detail)
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": c,
	})
}

// @Tags SysDictionaryDetail
// @Summary 用id查询SysDictionaryDetail
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionaryDetail true "用id查询SysDictionaryDetail"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sysDictionaryDetail/findSysDictionaryDetail [get]
func DictionaryDetailGetByID(c *gin.Context) {

	id := com.StrTo(c.Param("id")).MustInt()

	code := e.SUCCESS
	err, sysDictionary := models.GetSysDictionaryDetail(uint(id))
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": sysDictionary,
	})
}

// @Tags SysDictionaryDetail
// @Summary 分页获取SysDictionaryDetail列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysDictionaryDetailSearch true "页码, 每页大小, 搜索条件"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysDictionaryDetail/getSysDictionaryDetailList [get]
func DictionaryDetailGetList(c *gin.Context) {
	var pageInfo models.SysDictionaryDetailSearch
	_ = c.ShouldBindQuery(&pageInfo)

	data := make(map[string]interface{})
	code := e.SUCCESS

	err, list, total := models.GetSysDictionaryDetailInfoList(pageInfo)

	if err != nil {
		code = e.ERROR
	}
	data["data"] = list
	data["total"] = total
	data["page"] = pageInfo.Page
	data["pageSize"] = pageInfo.PageSize
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}
